<?php
class Zip extends ZipArchive
{
	/**
	 * 构造函数
	 *
	 */
	function __construct()
	{
	}
	
	/**
	 * 静态返回实例对象...
	 *
	 * @return Zip
	 */
	static function GetInstance()
	{
		return new Zip();
	}
	
	/**
	 * 创建 ZIP 文件。
	 *
	 * @param string $zipname
	 * @param string $dirname
	 */
	function Compress($zipname, $dirname)
	{
		$fi = pathinfo($dirname);
		
		$it = new RecursiveDirectoryIterator($dirname);
		
		$res = $this->open($zipname, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE);
		if ($res)
		{
			$this->addEmptyDir($fi['filename']);
			foreach (new RecursiveIteratorIterator($it) as $file)
			{
				$this->addFile($file, $fi['filename'] . '/' . str_replace('\\', '/', str_replace($dirname . '\\', '', $file)));
			}
			$this->close();
		}
	}
}
?>